<template>
  <div class="app-container">
    <!-- 条件栏 -->
    <div v-show="!dialogInfo.visible">
      <page-filter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      >
        <template v-slot:filter-button>
          <el-button
            v-if="$permission('receiveAddress:delete')"
            :disabled="!tableInfo.selections.length"
            @click="deleteAddress()"
            >删除</el-button
          >
        </template>
      </page-filter>
      <!-- 表格 -->
      <table-container
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="getReceiveAddressPage"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :row-class-name="tableRowClassName"
        @handleEvent="handleEvent"
      ></table-container>
    </div>
    <modify
      v-if="dialogInfo.visible"
      v-model:visible="dialogInfo.visible"
      :title="dialogInfo.title[dialogInfo.type]"
      :tableRow="dialogInfo.tableRow"
      @returnModify="returnModify"
    />
  </div>
</template>

<script setup name="receiveAddress">
import PageFilter from "@/components/PageFilter/index";
import TableContainer from "@/components/TableContainer/index";
import { getReceiveAddressPage, deleteReceiveAddress } from "@/api/core.js";
import modify from "./modify";
const {proxy} = getCurrentInstance() 
// 过滤相关配置
const filterInfo = reactive({
  query: {
    sort: "is_default desc, create_time desc",
  },
  list: [
    {
      type: "button",
      label: "新增",
      show: proxy.$permission("receiveAddress:add"),
      event: create,
    },
    { type: "slot", value: "button" },
  ],
});
// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  selections: [],
  fieldList: [
    { label: "地址", value: "addressValue" },
    { label: "联系人", value: "contacts" },
    { label: "联系电话", value: "contactsPhone" },
    { label: "是否默认", value: "isDefaultValue" },
  ],
  handle: {
    fixed: "right",
    label: "操作",
    width: '90',
    btList: [
      {
        label: "修改",
        event: update,
        show: proxy.$permission("receiveAddress:update"),
      },
      {
        label: "删除",
        event: deleteAddress,
        show: proxy.$permission("receiveAddress:delete"),
      },
    ],
  },
});
// 弹窗相关
const dialogInfo = reactive({
  title: {
    create: "添加",
    update: "修改",
  },
  tableRow: {},
  visible: false,
  type: "",
});
onMounted(() => {
  getList();
});

function returnModify() {
  if (dialogInfo.tableRow && dialogInfo.tableRow.id) {
    getList();
  } else {
    search();
  }
}
// 获取列表
function getList() {
  tableInfo.refresh = Math.random();
}
// 创建
function create() {
  dialogInfo.tableRow = {};
  dialogInfo.type = "create";
  dialogInfo.visible = true;
}
// 编辑
function update(data) {
  sessionStorage.setItem("storeRowId", data.id);
  dialogInfo.type = "update";
  dialogInfo.visible = true;
  dialogInfo.tableRow = {
    ...data,
    cities: [data.province, data.city, data.county],
  };
}
function tableRowClassName({ row }) {
  let rowId = sessionStorage.getItem("storeRowId");
  if (rowId && row.id === Number(rowId)) {
    return "check-row";
  }
  return "";
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case "tableCheck":
      tableInfo.selections = data;
      break;
  }
}

function search() {
  // 重置分页
  tableInfo.initCurpage = Math.random();
  tableInfo.refresh = Math.random();
}
function deleteAddress(row) {
  let arr = [];
  if (row && row.id) {
    arr = [row.id];
  } else {
    arr = tableInfo.selections.map((item) => item.id);
  }
  if (arr.length) {
    deleteReceiveAddress(arr).then((res) => {
      if (res) {
        search();
      }
    });
  }
}
</script>

<style scoped></style>
